package com.jwell.panprovider.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panprovider.contract.WaybillInfoContract
import com.jwell.panprovider.entity.WaybillInfoBean
import com.jwell.panprovider.model.WaybillInfoModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/4/24.
 * 运单详情
 */
class WaybillInfoPresenter : WaybillInfoContract.Presenter, BasePresenter<WaybillInfoContract.View>() {

    private val model: WaybillInfoModel by lazy {
        WaybillInfoModel()
    }

    override fun getData(id: Int) {
        getView()?.showProgress("", true)
        model.getData(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                var data = Gson().fromJson<WaybillInfoBean>(data.getString("result"), object : TypeToken<WaybillInfoBean>() {
                }.type)
                getView()?.showData(data)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.showDataFailed(msg)
            }
        })
    }

    override fun applyDelay(id: Int) {
        getView()?.showProgress("", true)
        model.applyDelay(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                getView()?.toast("已申请延期")
                getData(id)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast("申请延期失败" + msg)
            }
        })
    }

    override fun revokeWaybill(id: Int) {
        getView()?.showProgress("", true)
        model.revokeWaybill(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                getView()?.toast("已撤销")
                getData(id)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast("撤销失败" + msg)
            }
        })
    }

}